package com.thssh.ui.lsn05.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.thssh.ui.R;

/**
 * @author zhangyugehu
 * @version V1.0
 * @date 2017/09/15
 * email: zhangyugehu@163.com
 */

public class BitmapXfermodeView extends View {

    private Bitmap mSrcRectBitmap;
    private Bitmap mDstCircleBitmap;

    private Paint mPaint;
    private int mMode;
    private PorterDuff.Mode xMode;
    private String mModeName;

    private static final int DEF_WIDTH = 200;
    private int mWidth;
    private int mHeight;

    private int mTextHeight = 30;


    public BitmapXfermodeView(Context context) {
        this(context, null);
    }

    public BitmapXfermodeView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BitmapXfermodeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setTextSize(30);
        mPaint.setTextAlign(Paint.Align.CENTER);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.XfermodeView);
        mMode = typedArray.getInt(R.styleable.XfermodeView_mode, 0);
        xMode = getXfermodeMode();
        typedArray.recycle();
    }

    private void createBitmaps() {
        int[] allGreenColors = new int[mWidth*mHeight];
        int[] allRedColors = new int[mWidth*mHeight];
        for(int i = 0; i < mWidth; i++){
            for(int j = 0; j < mHeight; j++){
                int index = i * mWidth + j;
                if(i < mWidth / 9 || i > mWidth * 6 / 9){
                    allGreenColors[index] = Color.TRANSPARENT;
                }else if(j < mHeight / 9 || j > mHeight * 6 /9){
                    allGreenColors[index] = Color.TRANSPARENT;
                }else{
                    allGreenColors[index] = Color.GREEN;
                }
                if(i < mWidth / 3 || i > mWidth * 8 / 9){
                    allRedColors[index] = Color.TRANSPARENT;
                }else if(j < mHeight / 3 || j > mHeight * 8 / 9){
                    allRedColors[index] = Color.TRANSPARENT;
                }else{
                    allRedColors[index] = Color.RED;
                }
            }
        }
        mSrcRectBitmap = Bitmap.createBitmap(allGreenColors, mWidth, mHeight, Bitmap.Config.ARGB_8888);
        mDstCircleBitmap = Bitmap.createBitmap(allRedColors, mWidth, mHeight, Bitmap.Config.ARGB_8888);
    }

    private PorterDuff.Mode getXfermodeMode() {
        PorterDuff.Mode xMode = PorterDuff.Mode.CLEAR;
        mModeName = "CLEAR[0, 0]";
        switch (mMode){
            case 0:
                xMode = PorterDuff.Mode.CLEAR;
                mModeName = "CLEAR";
                break;
            case 1:
                xMode = PorterDuff.Mode.SRC;
                mModeName = "SRC";
                break;
            case 2:
                xMode = PorterDuff.Mode.DST;
                mModeName = "DST";
                break;
            case 3:
                xMode = PorterDuff.Mode.SRC_OVER;
                mModeName = "SRC_OVER";
                break;
            case 4:
                xMode = PorterDuff.Mode.DST_OVER;
                mModeName = "DST_OVER";
                break;
            case 5:
                xMode = PorterDuff.Mode.SRC_IN;
                mModeName = "SRC_IN";
                break;
            case 6:
                xMode = PorterDuff.Mode.DST_IN;
                mModeName = "DST_IN";
                break;
            case 7:
                xMode = PorterDuff.Mode.SRC_OUT;
                mModeName = "SRC_OUT";
                break;
            case 8:
                xMode = PorterDuff.Mode.DST_OUT;
                mModeName = "DST_OUT";
                break;
            case 9:
                xMode = PorterDuff.Mode.SRC_ATOP;
                mModeName = "SRC_ATOP";
                break;
            case 10:
                xMode = PorterDuff.Mode.DST_ATOP;
                mModeName = "DST_ATOP";
                break;
            case 11:
                xMode = PorterDuff.Mode.XOR;
                mModeName = "XOR";
                break;
            case 12:
                xMode = PorterDuff.Mode.ADD;
                mModeName = "ADD";
                break;
            case 13:
                xMode = PorterDuff.Mode.MULTIPLY;
                mModeName = "MULTIPLY";
                break;
            case 14:
                xMode = PorterDuff.Mode.SCREEN;
                mModeName = "SCREEN";
                break;
            case 15:
                xMode = PorterDuff.Mode.OVERLAY;
                mModeName = "OVERLAY";
                break;
            case 16:
                xMode = PorterDuff.Mode.DARKEN;
                mModeName = "DARKEN";
                break;
            case 17:
                xMode = PorterDuff.Mode.LIGHTEN;
                mModeName = "LIGHTEN";
                break;
            default:
                xMode = null;
                mModeName = "NONE";
                break;
        }
        return xMode;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if(MeasureSpec.getMode(widthMeasureSpec) ==MeasureSpec.EXACTLY){
            mWidth = MeasureSpec.getSize(widthMeasureSpec);
            mHeight = mWidth;
        }else{
            mWidth = mHeight = DEF_WIDTH;
        }
        setMeasuredDimension(mWidth, mHeight + mTextHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        createBitmaps();
        canvas.drawBitmap(mDstCircleBitmap, 0, 0, mPaint);
        if(xMode != null) {
            mPaint.setXfermode(new PorterDuffXfermode(xMode));
        }
        canvas.drawBitmap(mSrcRectBitmap, 0, 0, mPaint);
        mPaint.setXfermode(null);
        mPaint.setStyle(Paint.Style.STROKE);
//        mPaint.setStrokeWidth(10);
        canvas.drawRect(0, 0, mWidth, mHeight, mPaint);
        mPaint.setStyle(Paint.Style.FILL);
//        mPaint.setStrokeWidth(5);
        canvas.drawText(mModeName, mWidth / 2, mHeight + mTextHeight, mPaint);
    }
}
